package com.yunhe.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.yunhe.entity.Category;
import com.yunhe.entity.Goods;
import com.yunhe.entity.Supplier;
import com.yunhe.util.MyBatisUtil;

public class GoodsImpl implements IGoods {

	SqlSession ss = MyBatisUtil.getSession();
	@Override
	public int addGoods(Goods goods,Category category,Supplier supplier) {
		
		Map map = new HashMap();
		map.put("gname", goods.getGname());
		map.put("price",goods.getPrice());
		map.put("reserve", goods.getReserve());
		map.put("cid", category.getCid());
		map.put("sid", supplier.getSid());
		int count = ss.insert("com.yunhe.dao.IGoods.addGoods", map);
		ss.commit();
		ss.close();
		return count;
	}
	@Override
	public int updateGoods(Goods goods, Category category, Supplier supplier) {
		Map map = new HashMap();
		map.put("gname", goods.getGname());
		map.put("price",goods.getPrice());
		map.put("reserve", goods.getReserve());
		map.put("id", goods.getId());
		map.put("cid", category.getCid());
		map.put("sid", supplier.getSid());
		int count = ss.update("com.yunhe.dao.IGoods.updateGoods", map);
		ss.commit();
		ss.close();
		return count;
	}
	@Override
	public int delGoods(Goods goods) {
		int count = ss.delete("com.yunhe.dao.IGoods.delGoods", goods);
		ss.commit();
		ss.close();
		return count;
	}
	@Override
	public List<Map> getGoodsByPage(int pageIndex, int pageSize) {
		Map mapp = new HashMap();
		mapp.put("start", (pageIndex-1)*pageSize);
		mapp.put("size", pageSize);
		List<Map> map = ss.selectList("com.yunhe.dao.IGoods.getGoodsByPage", mapp);
		ss.close();
		return map;
	}
	@Override
	public int updateGoodsBygname(Goods goods) {
		int count = ss.update("com.yunhe.dao.IGoods.updateGoodsBygname", goods);
		ss.commit();
		ss.close();
		return count;
	}

}
